package com.sgb.kjwl.view.ui.fragment.transport;

import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMyCarriageManageBinding;
import com.sgb.kjwl.model.entity.self.CarriageInfoPublishEntity;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.activity.transport.CarriageDetailActivity;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.CarriageManageAdapter;
import com.sgb.kjwl.viewmodel.self.carMakers.MyCarriageManageModel;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.common.base.BaseFragment;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/12/30
 * Author:黄淋
 * Description:
 */
public class MyCarriageManageFragment extends BaseFragment<MyCarriageManageModel, FragmentMyCarriageManageBinding> {

    private CarriageManageAdapter carriageManageAdapter;
    private int messageStatus;//信息状态 1正常 2失效 null全部

    public static MyCarriageManageFragment newInstance(int type) {
        MyCarriageManageFragment fragment = new MyCarriageManageFragment();
        Bundle args = new Bundle();
        args.putInt(RouterKey.TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_my_carriage_manage;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(MyCarriageManageModel.class);
    }

    @Override
    protected void init() {
        initAdapter();
        messageStatus = getArguments().getInt(RouterKey.TYPE);
        mViewModel.myCarrierList(getContext(), false, messageStatus);
        mViewModel.liveData.observe(this, this::showList);
        mViewModel.liveDataMore.observe(this, this::lodMore);
    }

    private void initAdapter() {
        if (carriageManageAdapter == null) {
            carriageManageAdapter = new CarriageManageAdapter(getActivity(), null);
        }
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divider_5dp));
        mBinding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        RecyclerView.LayoutParams params = layoutManager.generateDefaultLayoutParams();
        params.setMargins(0, 15, 0, 0);
        mBinding.recyclerView.setAdapter(carriageManageAdapter);
        mBinding.showView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setFootViewText("加载中", "没有更多数据了");
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.myCarrierList(getContext(), false, messageStatus);
            }

            @Override
            public void onLoadMore() {
                mViewModel.myCarrierList(getContext(), true, messageStatus);
            }
        });

        carriageManageAdapter.setOnItemClick(new AdapterOnItemClick<CarriageInfoPublishEntity>() {
            @Override
            public void onItemClick(CarriageInfoPublishEntity carriageManageEntity, int position) {
                CarriageDetailActivity.start(getActivity(), carriageManageEntity.getNo(), 2);
            }
        });
    }

    public void showList(ListResponse<List<CarriageInfoPublishEntity>> list) {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
        if (list.getList().size() > 0) {
            carriageManageAdapter.setDatas(list.getList());
            mBinding.emptyView.setVisibility(View.GONE);
        } else {
            carriageManageAdapter.setDatas(null);
            mBinding.emptyView.setVisibility(View.VISIBLE);
        }
        mBinding.recyclerView.setNoMore(!list.isLastPage());
    }


    public void lodMore(ListResponse<List<CarriageInfoPublishEntity>> list) {

        mBinding.recyclerView.refreshComplete();
        if (list == null || list.getList() == null)
            return;
        if (list.getList().size() > 0) {
            carriageManageAdapter.addDatas(list.getList());
        }
        mBinding.recyclerView.setNoMore(list.isLastPage());
    }
}
